// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-12-20

#include "stdafx.h"
#include "svr_pingecho.h"

namespace JoyServer
{

// class CJoyPingEchoSocket

CJoyPingEchoSocket::CJoyPingEchoSocket()
{
}

CJoyPingEchoSocket::~CJoyPingEchoSocket()
{
}

bool CJoyPingEchoSocket::Attach(SOCKET s, bool bStream, int& err)
{
    if(!__super::Attach(s, bStream, err))
    {
        return false;
    }
    // we recv small datas. just 1024 bytes.
    m_recvBufLen    = 1024;
    return true;
}

void CJoyPingEchoSocket::OnError(int err)
{
    err;
}

void CJoyPingEchoSocket::OnRecv(CJoyTransmitData* recvData, const sockaddr_in& fromAddr)
{
    SendTo(recvData, fromAddr);
}

bool CJoyPingEchoSocket::Create(const sockaddr_in& addr)
{
    if(!__super::Create(false))
    {
        return false;
    }
    if(!Bind(addr))
    {
        Close();
        return false;
    }

    // queue 16 recv operations to recv UDP data.
    for(int i = 0; i < 16; ++ i)
    {
        QueueRecv();
    }

    return true;
}

bool CJoyPingEchoSocket::Create(const char* ip, int port)
{
    sockaddr_in addr;
    MakeAddress(addr, ip, port);
    return Create(addr);
}

} // namespace JoyServer